package com.hhxy.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * 获取Cookie
 */
@WebServlet("/BServlet")
public class BServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取Cookie数组
        Cookie[] cookies = request.getCookies();
        //2、遍历Cookie数组
        for (int i=0;i<cookies.length;i++) {
            //3、获取Cookie对象中的数据
            String name = cookies[i].getName();
            if(name.equals("username")){
                String value = cookies[i].getValue();
                //ULR解码
                String encode = URLDecoder.decode(value, "UTF-8");
                System.out.println(name+":"+encode);
            }
        }
    }
}
